package com.songcha.library_common.util

import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.core.graphics.drawable.toBitmap
import androidx.core.graphics.drawable.toDrawable
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.target.Target
import com.bumptech.glide.request.transition.Transition
import com.songcha.library_common.R


class GlideUtil {
    fun interface IOnGlideLoadListener{
        fun onGlideLoadSuccess(bitmap:Bitmap)
        fun onGlideLoadError(e:GlideException?){}
    }

    companion object{
        @JvmStatic
        fun loadNetImage(iv:ImageView,url:String?,diskCacheStrategy:DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC){
            if(url.isNullOrBlank()) return
            Glide.with(iv.context).load(url)
                .diskCacheStrategy(diskCacheStrategy)
                .error(R.drawable.common_bg_image_error)
                .into(iv)
        }

       /* @JvmStatic
        fun loadNetImageByTarget(context: Context,url:String?,target:CustomTarget<Bitmap>,diskCacheStrategy:DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC){
            if(url.isNullOrBlank()) return
            Glide.with(context).asBitmap()
                .load(url)
                .diskCacheStrategy(diskCacheStrategy)
                .into(target)
        }*/

        @JvmStatic
        fun loadNetImageListener(url:String?,iv:ImageView,isPreventOnResourceReady:Boolean, diskCacheStrategy:DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC,
                                 listener:IOnGlideLoadListener){

            if(url.isNullOrBlank()) return
            Glide.with(iv.context).load(url)
                .diskCacheStrategy(diskCacheStrategy)
                .error(R.drawable.common_bg_image_error)
                //bitmap bitmap is dependency imageview size
                .listener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        listener.onGlideLoadError(e)
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        resource?.let {
                           listener.onGlideLoadSuccess(it.toBitmap())
                        }
                        return isPreventOnResourceReady
                    }

                })
                .into(iv)
        }


        @JvmStatic
        fun getRawImageBitmap(context:Context,url:String,diskCacheStrategy:DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC,block:(Bitmap)->Unit){
            if(url.isNullOrBlank()) return
            val target = object : CustomTarget<Bitmap>(){
                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    block(resource)
                }

                override fun onLoadCleared(placeholder: Drawable?) {

                }

            }

            Glide.with(context).asBitmap().load(url)
                .diskCacheStrategy(diskCacheStrategy)
                .into(target)
        }

        @JvmStatic
        fun loadCustomImageView(iv: ImageView,url:String,width:Int,
                                diskCacheStrategy:DiskCacheStrategy = DiskCacheStrategy.AUTOMATIC,onGetLayoutParams:(width:Int,height:Int)->ViewGroup.LayoutParams, onHeight:((height:Int)->Unit)? = null){
            if(url.isNullOrBlank()) return
            val target = object : CustomViewTarget<ImageView,Bitmap>(iv){
                override fun onLoadFailed(errorDrawable: Drawable?) {

                }

                override fun onResourceCleared(placeholder: Drawable?) {

                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                    val height = (width / (resource.width * 1.0f / resource.height)).toInt()
                    //val lp = (P as ViewGroup).LayoutParams(width,height)
                    iv.layoutParams = onGetLayoutParams(width,height)
                    iv.setImageBitmap(resource)
                    if(onHeight != null)
                        onHeight(height)
                }

            }
            Glide.with(iv.context).asBitmap().load(url)
                .diskCacheStrategy(diskCacheStrategy)
                .into(target)
        }


    }
}
